package com.tbit.uqbike.constant;


import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 故障类型
 *
 * @Author LHX
 * @ClassName FaultType
 * @Description
 * @date 2023/5/19 11:29
 * @Version 1.0
 */
public class FaultType {


    //运维端

    public static final String OTHER = "其他";
    public static final String TYPE_OTHER = "0";

    public static final String BRAKE = "刹车";
    public static final String TYPE_BRAKE = "1";

    public static final String HANDLEBAR = "转把手";
    public static final String TYPE_HANDLEBAR = "2";

    public static final String KICKSTAND = "车撑";
    public static final String TYPE_KICKSTAND = "3";

    public static final String QR_CODE = "二维码";
    public static final String TYPE_QR_CODE = "4";

    public static final String CONTROLLER = "中控";
    public static final String TYPE_CONTROLLER = "5";

    public static final String SPEAKER = "喇叭故障";
    public static final String TYPE_SPEAKER = "6";

    public static final String BATTERY = "电池";
    public static final String TYPE_BATTERY = "7";

    public static final String BATTERY_LOCK = "电池锁";
    public static final String TYPE_BATTERY_LOCK = "8";

    public static final String HELMET_LOCK = "头盔锁";
    public static final String TYPE_HELMET_LOCK = "9";


    //用户端

    public static final String OTHER_USER = "其他";
    public static final String TYPE_OTHER_USER = "0";

    public static final String HAVE_ELECTRICITY_USER = "有电车不走";
    public static final String TYPE_HAVE_ELECTRICITY_USER = "1";

    public static final String BRAKE_FAILURE_USER = "刹车不灵";
    public static final String TYPE_BRAKE_FAILURE_USER = "2";

    public static final String SEAT_CUSHION_DAMAGED_USER = "座垫损坏";
    public static final String TYPE_SEAT_CUSHION_DAMAGED_USER = "3";

    public static final String BRACE_DAMAGE_USER = "车撑损坏";
    public static final String TYPE_BRACE_DAMAGE_USER = "4";

    public static final String QR_CODE_DAMAGE_USER = "二维码损坏";
    public static final String TYPE_QR_CODE_DAMAGE_USER = "5";

    public static final String HELMET_DAMAGE_USER = "头盔故障";
    public static final String TYPE_HELMET_DAMAGE_USER = "6";

    public static final String SPEAKER_USER = "喇叭故障";
    public static final String TYPE_SPEAKER_USER = "7";
    /**
     * 维修记录的故障类型
     */
    static final Map<String, String> REPAIR_ABNORMAL = new HashMap<>();

    static {
        REPAIR_ABNORMAL.put("0", "转把手");
        REPAIR_ABNORMAL.put("1", "车灯");
        REPAIR_ABNORMAL.put("2", "线路");
        REPAIR_ABNORMAL.put("3", "车篮");
        REPAIR_ABNORMAL.put("4", "车轮");
        REPAIR_ABNORMAL.put("5", "车撑");
        REPAIR_ABNORMAL.put("6", "刹车");
        REPAIR_ABNORMAL.put("7", "车座");
        REPAIR_ABNORMAL.put("8", "二维码");
        REPAIR_ABNORMAL.put("9", "电池");
        REPAIR_ABNORMAL.put("10", "脚蹬");
        REPAIR_ABNORMAL.put("11", "前叉");
        REPAIR_ABNORMAL.put("12", "挡泥板");
        REPAIR_ABNORMAL.put("13", "控制器");
        REPAIR_ABNORMAL.put("14", "中控");
        REPAIR_ABNORMAL.put("15", "响铃铛");
        REPAIR_ABNORMAL.put("16", "电池锁");
        REPAIR_ABNORMAL.put("50", "其他");
    }

    /**
     * 匹配运维上传维修记录异常记录类型
     */
    public static String matchFaultType(String faults) {
        if (StringUtils.isBlank(faults)) {
            return "";
        }
        List<String> faultsList = Arrays.stream(faults.split(","))
                .map(REPAIR_ABNORMAL::get)
                .collect(Collectors.toList());
        return StringUtils.join(faultsList, ",");
    }

    public static String parseFaults(String faults) {
        if (StringUtils.isBlank(faults)) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : faults.split(",")) {
            switch (s) {
                case FaultType.TYPE_OTHER:
                    stringBuilder.append(FaultType.OTHER).append(",");
                    break;
                case FaultType.TYPE_BRAKE:
                    stringBuilder.append(FaultType.BRAKE).append(",");
                    break;
                case FaultType.TYPE_HANDLEBAR:
                    stringBuilder.append(FaultType.HANDLEBAR).append(",");
                    break;
                case FaultType.TYPE_KICKSTAND:
                    stringBuilder.append(FaultType.KICKSTAND).append(",");
                    break;
                case FaultType.TYPE_QR_CODE:
                    stringBuilder.append(FaultType.QR_CODE).append(",");
                    break;
                case FaultType.TYPE_CONTROLLER:
                    stringBuilder.append(FaultType.CONTROLLER).append(",");
                    break;
                case FaultType.TYPE_SPEAKER:
                    stringBuilder.append(FaultType.SPEAKER).append(",");
                    break;
                case FaultType.TYPE_BATTERY:
                    stringBuilder.append(FaultType.BATTERY).append(",");
                    break;
                case FaultType.TYPE_BATTERY_LOCK:
                    stringBuilder.append(FaultType.BATTERY_LOCK).append(",");
                    break;
                case FaultType.TYPE_HELMET_LOCK:
                    stringBuilder.append(FaultType.HELMET_LOCK).append(",");
                    break;
                default:
                    stringBuilder.append(s);
                    break;
            }
        }
        String s = stringBuilder.toString();
        if (s.endsWith(",") && StringUtils.isNotBlank(s)) {
            return s.substring(0, s.length() - 1);
        }
        return s;
    }

    /**
     * 解析故障（带故障二字），但不解析用户上报故障
     */
    public static String parseFaultsExceptUser(String faults) {
        if (StringUtils.isBlank(faults)) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : faults.split(",")) {
            switch (s) {
                case FaultType.TYPE_OTHER:
                    stringBuilder.append(FaultType.OTHER).append("故障").append(",");
                    break;
                case FaultType.TYPE_BRAKE:
                    stringBuilder.append(FaultType.BRAKE).append("故障").append(",");
                    break;
                case FaultType.TYPE_HANDLEBAR:
                    stringBuilder.append(FaultType.HANDLEBAR).append("故障").append(",");
                    break;
                case FaultType.TYPE_KICKSTAND:
                    stringBuilder.append(FaultType.KICKSTAND).append("故障").append(",");
                    break;
                case FaultType.TYPE_QR_CODE:
                    stringBuilder.append(FaultType.QR_CODE).append("故障").append(",");
                    break;
                case FaultType.TYPE_CONTROLLER:
                    stringBuilder.append(FaultType.CONTROLLER).append("故障").append(",");
                    break;
                case FaultType.TYPE_SPEAKER:
                    stringBuilder.append(FaultType.SPEAKER).append(",");
                    break;
                case FaultType.TYPE_BATTERY:
                    stringBuilder.append(FaultType.BATTERY).append("故障").append(",");
                    break;
                case FaultType.TYPE_BATTERY_LOCK:
                    stringBuilder.append(FaultType.BATTERY_LOCK).append("故障").append(",");
                    break;
                case FaultType.TYPE_HELMET_LOCK:
                    stringBuilder.append(FaultType.HELMET_LOCK).append("故障").append(",");
                    break;
            }
        }
        String s = stringBuilder.toString();
        if (s.endsWith(",") && StringUtils.isNotBlank(s)) {
            return s.substring(0, s.length() - 1);
        }
        return s;
    }

}
